package com.laobiao.file_netty_lib.netty.server.handler;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import com.laobiao.file_netty_lib.exception.FileBaseException;
import com.laobiao.file_netty_lib.netty.server.ServerApplication;
import com.laobiao.file_netty_lib.netty.server.ServerBusiness;
import com.laobiao.file_netty_lib.netty.server.ServerHandler;
import com.laobiao.file_netty_lib.packet.BasePacket;
import com.laobiao.file_netty_lib.packet.command.ResoultCommand;
import com.laobiao.file_netty_lib.packet.command.filelist.FileListCommand;
import com.laobiao.file_netty_lib.packet.data.BaseDataPacket;
import com.laobiao.file_netty_lib.util.ByteUtil;
import com.laobiao.file_netty_lib.util.FileUtil;

import io.netty.channel.ChannelHandlerContext;
/**
 * 服务端文件遍历业务类
 * @author 38079
 *
 */
public class ServerFileListBusiness implements ServerBusiness{

	/**
	 * 主业务命令
	 */
	private FileListCommand command;
	
	/**
	 * 要遍历的文件夹
	 */
	private File dir;
	
	public ServerFileListBusiness(ChannelHandlerContext ctx,FileListCommand command) {
		this.command=command;
		String dirPath = command.getRemoteDirPath();
		if(dirPath==null) {
			ResoultCommand rc =new ResoultCommand(ResoultCommand.ResoultCode.Common.COMM_RESOULT, "目录文件地址不能为空！", false, command.getBusinessGroup());
			ctx.writeAndFlush(rc);
			close();
			throw new FileBaseException("目录文件地址不能为空！", FileBaseException.ERR_PARAM_NULL);
		}else {
			dir=new File(ServerApplication.getServerBasePath()+dirPath);
			if(!dir.exists() || !dir.isDirectory()) {
				ResoultCommand rc =new ResoultCommand(ResoultCommand.ResoultCode.Common.COMM_RESOULT, "目录文件地址不存在！", false, command.getBusinessGroup());
				ctx.writeAndFlush(rc);
				close();
				throw new FileBaseException("目录文件地址不存在！", FileBaseException.ERR_PARAM_NULL);
			}
			try {
				sendData(ctx);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ResoultCommand rc =new ResoultCommand(ResoultCommand.ResoultCode.Common.COMM_RESOULT, "解析文件失败！", false, command.getBusinessGroup());
				ctx.writeAndFlush(rc);
				close();
				throw new FileBaseException("目录文件地址不存在！", e,FileBaseException.ERR_PARAM_NULL);
			}
		}
	}
	
	private void sendData(ChannelHandlerContext ctx) throws UnsupportedEncodingException {
		File[] childs = dir.listFiles();
		StringBuilder sbf=new StringBuilder();
		for (File file : childs) {
			sbf.append(file.getName()+FileListCommand.DETAIL_SPLIT_CHARACTER+(file.isDirectory()?FileListCommand.FILE_TYPE_DIR:FileListCommand.FILE_TYPE_FILE)+FileListCommand.FILE_SPLIT_CHARACTER);
		}
		byte[] bytes = sbf.toString().getBytes("UTF-8");
		int surplusLength = bytes.length;//剩余长度
		if(surplusLength==0) {
			//空目录
			BaseDataPacket packet=new BaseDataPacket(command.getBusinessGroup()," ".getBytes());
			ctx.writeAndFlush(packet);
			ResoultCommand rc =new ResoultCommand(ResoultCommand.ResoultCode.Common.COMM_RESOULT, "目录文件遍历完成！", true, command.getBusinessGroup());
			ctx.writeAndFlush(rc);
			return;
		}
		int from=0;
		int to=(int)Math.min(surplusLength, 1015);
		//byte[] b=new byte[to];
		
		while (surplusLength>0) {
			byte[] copyOfRange = Arrays.copyOfRange(bytes, from, to);
			//String data = sbf.substring(from, to);
			
			
			BaseDataPacket packet=new BaseDataPacket(command.getBusinessGroup(),copyOfRange);
			ctx.writeAndFlush(packet);
			surplusLength-=1015;
			int bSize=(int)Math.min(surplusLength, 1015);
			if(bSize<=0) {
				break;
			}else {
				from=to;
				to=Math.min(to+1015,bytes.length);
			}
		}
		ResoultCommand rc =new ResoultCommand(ResoultCommand.ResoultCode.Common.COMM_RESOULT, "目录文件遍历完成！", true, command.getBusinessGroup());
		ctx.writeAndFlush(rc);
	}
	
	@Override
	public void close() {
	}

	@Override
	public void hasPacket(ChannelHandlerContext ctx, BasePacket packet) {
		//无处理项
	}

}
